package com.bwie.parkdemo.mvvm.viewmodel;

import android.app.Application;
import android.databinding.BaseObservable;
import android.databinding.ObservableField;
import android.os.Bundle;
import android.support.annotation.NonNull;

import com.bwie.lib_core.action.ActActionField;
import com.bwie.lib_core.base.BaseEntity;
import com.bwie.lib_core.mvvm.base.BaseOBViewModel;
import com.bwie.lib_core.mvvm.base.BaseViewModel;
import com.bwie.parkdemo.mvvm.model.VisitorValuesModel;
import com.bwie.parkdemo.mvvm.model.entity.LoginEntity;
import com.bwie.parkdemo.mvvm.model.entity.VisitorValuesEntity;
import com.bwie.parkdemo.mvvm.ui.activity.AddVisitorActivity;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 预约申请详情
 * */
public class VisitorValuesViewModel extends BaseOBViewModel<VisitorValuesModel> {

    public String data;//来访起止日期->上级界面传入
    public String time;//来访时长->上级界面获取
    public ObservableField<String>userName;//来访人员->model中获取
    public ObservableField<String>pId;//来访部门->model中获取
    public ObservableField<String> nowTime;//申请时间->本机界面获取
    public String carNum;//车牌照->上级界面获取
    public String parkId = "0";//车位->上级界面获取
    //描述->上级界面传入
    private int applyId = 0;//申请id

    public VisitorValuesViewModel(@NonNull Application application) {
        super(application);
    }

    public void setApplyId(int applyId) {
        this.applyId = applyId;
    }

    public void setData(String data) {
        this.data = data;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public void setCarNum(String carNum) {
        this.carNum = carNum;
    }

    public void setParKId(String parkId) {
        this.parkId = parkId;
    }

    @Override
    protected VisitorValuesModel createModel() {
        return new VisitorValuesModel();
    }

    @Override
    protected void initData() {
        //text R.string.xxx
        //添加单向绑定的用户详细信息
        requestVisitorValues();
        //获取model层用户信息数据
        LoginEntity.Values user = m.requestUser(getApplication());
        userName = new ObservableField<>();
        userName.set(""+user.uId);
        pId = new ObservableField<>();
        pId.set(""+user.pId);
    }

    //获取来访预约详情接口数据
    private void requestVisitorValues(){

        Map<String,Object>map = new HashMap<>();
        map.put("applyId",applyId);
        String jsonStr = new Gson().toJson(map);
        RequestBody body = RequestBody.create(MediaType.parse("application/json"),jsonStr);
        m.request(body).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    //跳转到添加申请界面
    public void startAddVisitor(){
        Map<String,Object>map = new HashMap<>();
        map.put(ActActionField.ACTCLAZZ, AddVisitorActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt("parkId",Integer.valueOf(parkId));
        bundle.putString("carNum",carNum);
        bundle.putString("partName","占位符");
        bundle.putString("describe","占位符");
        bundle.putString("startTime","开始时间");
        bundle.putString("endTime","结束时间");
        bundle.putString("nowTime",nowTime.get());
        map.put(ActActionField.ACTBUNDLE,bundle);
        startAct(map);
        finishAct();
    }

    @Override
    public void onNext(BaseEntity entity) {
        VisitorValuesEntity valuesEntity = (VisitorValuesEntity) entity;
        nowTime = new ObservableField<>();
        nowTime.set(valuesEntity.getValuesEntity().visitorTime);
    }
}
